package com.example.dilidili_ting.view;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.example.dilidili_ting.R;
import com.example.dilidili_ting.activity.MovieDetailsActivityTing;
import com.example.dilidili_ting.activity.SearchActivityTing;
import com.example.dilidili_ting.activity.hyActivityTing;
import com.example.dilidili_ting.activity.regActivityTing;
import com.example.dilidili_ting.adapter.HomeAdapterTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.IMGBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.bean.MovieTypeTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.AlphaPageTransformer;
import com.youth.banner.transformer.RotateYTransformer;
import com.youth.banner.transformer.ScaleInTransformer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.example.dilidili_ting.utils.OkhttpUtilsTing.JSON;

public class HomeViewTing {
    private FragmentActivity ContextTing;
    private LayoutInflater inflaterTing;
    private View currentViewTing;
    private Banner bannerTing;
    private RelativeLayout userInfoTing;
    private TextView tvUsernameTing;
    private ImageView ivMyBtnTing;
    private GridView gridViewTing;
    private List<IMGBeanTing> listTing;
    private List<MovieTypeTing> movieTypeTing;
    private BaseAdapter adapterTing;
    private String count;
    Gson gson = new Gson();

    public HomeViewTing(FragmentActivity context){
        ContextTing = context;
        //为之后将Layout转化为view时用
        inflaterTing = LayoutInflater.from(context);
    }

    private void createView(){
        initBannerData();
        initMovieData();
        initView();
    }

    private void initView() {
        currentViewTing=inflaterTing.inflate(R.layout.view_home_ting,null);
        bannerTing = (Banner) currentViewTing.findViewById(R.id.banner_ting);
        userInfoTing = (RelativeLayout) currentViewTing.findViewById(R.id.user_info_ting);
        tvUsernameTing = (TextView) currentViewTing.findViewById(R.id.tv_username_ting);
        ivMyBtnTing = (ImageView) currentViewTing.findViewById(R.id.iv_my_btn_ting);
        gridViewTing = (GridView) currentViewTing.findViewById(R.id.grid_view_ting);
        adapterTing=new HomeAdapterTing(ContextTing,movieTypeTing);
        gridViewTing.setAdapter(adapterTing);
        ivMyBtnTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2021/6/17  点击进入搜索页面
                Intent intent=new Intent(ContextTing, SearchActivityTing.class);
                ContextTing.startActivity(intent);
            }
        });
    }

    /**
     * 初始化轮播图数据
     */
    private void initBannerData() {
        listTing = new ArrayList<>();
        OkhttpUtilsTing.isGET("banner/getImage", true, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                JsonParser parser = new JsonParser();
//                JsonElement element =parser.parse(response.body().string());
//                JsonArray data = element.getAsJsonObject().get("data").getAsJsonArray();
//                Integer code = element.getAsJsonObject().get("code").getAsInt();
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<IMGBeanTing>> r = gson.fromJson(res, new TypeToken<DataBeanTing<List<IMGBeanTing>>>(){}.getType());
                if(r.code == 200){
                    for (int i = 0; i < r.data.size(); i++) {
//                        listTing.add(new IMGBeanTing(data.get(i).getAsJsonObject().get("id").getAsInt(),data.get(i).getAsJsonObject().get("movieName").getAsString(),data.get(i).getAsJsonObject().get("bannerUrl").getAsString()));
                        listTing.add(r.data.get(i));
                    }
                }
                System.out.println("数据"+r.data);
                ContextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bannerTing.setAdapter(new BannerImageAdapter<IMGBeanTing>(listTing) {
                            @Override
                            public void onBindView(BannerImageHolder holder, IMGBeanTing data, int position, int size) {
                                Glide.with(holder.itemView)
                                        .load(data.bannerUrl)
                                        .into(holder.imageView);
                                Gson g = new Gson();
//                                Log.i("hhh",g.toJson(data));
                            }
                        })
                                .isAutoLoop(true)
                                .setIndicator(new CircleIndicator(ContextTing))
                                .setIndicatorSelectedColor(Color.parseColor("#fb7f40"))
                                // 滑动样式
//                              .addPageTransformer(new ZoomOutPageTransformer())
                                .addPageTransformer(new RotateYTransformer())
                                .addPageTransformer(new ScaleInTransformer())
                                .addPageTransformer(new AlphaPageTransformer())
//                              .setBannerGalleryMZ(20); //魅影效果
                                .setBannerGalleryEffect(20, 10); //画廊效果

//                        banner点击事件
                        bannerTing.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object o, int i) {
                                // TODO: 2021/6/17 调接口传入信息,获取电影详情,并跳转页面
                                String obj = new Gson().toJson(o);
                                IMGBeanTing img = gson.fromJson(obj, new TypeToken<IMGBeanTing>(){}.getType());
                                Intent intent=new Intent(ContextTing, MovieDetailsActivityTing.class);
                                Log.i("电影名：",img.movieName);
                                intent.putExtra("movieName",img.movieName);
                                ContextTing.startActivity(intent);
                            }
                        });
                    }
                });
            }
        });
    }

    /**
     * 初始化电影信息数据
     */
    private void initMovieData() {
        movieTypeTing=new ArrayList<>();

        /**
         *获取电影信息接口
         */
        movieTypeTing.add(new MovieTypeTing(R.drawable.juqing,"剧情","5"));
        movieTypeTing.add(new MovieTypeTing(R.drawable.xiaoku,"喜剧","4"));
        movieTypeTing.add(new MovieTypeTing(R.drawable.langman,"浪漫","3"));
        movieTypeTing.add(new MovieTypeTing(R.drawable.zhizhux,"科幻","3"));
        movieTypeTing.add(new MovieTypeTing(R.drawable.katong,"动画","3"));
        movieTypeTing.add(new MovieTypeTing(R.drawable.kongbu,"悬疑","1"));
    }

    /**
     * 初始化电影信息条数数据
     */
    public String movieCount(String type){
        count = "";
        JsonObject jo = new JsonObject();
        jo.addProperty("type",type);
           OkhttpUtilsTing.isPOST("movie/getMovies",true,jo,new Callback() {
               @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<MovieBeanTing>> r = gson.fromJson(res, DataBeanTing.class);
                count = gson.toJson(r.data.size());
                ContextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("count",count);
                        movieTypeTing.add(new MovieTypeTing(R.drawable.alien,"剧情",movieCount("剧情")));
                    }
                });
            }
        });
        return count;
    }

    /**
     * 显示当前导航栏上方所对应的view界面
     */
    public void showView(){
        if (currentViewTing == null){
            createView();
        }
        currentViewTing.setVisibility(View.VISIBLE);
    }

    /**
     * 获取当前在导航栏上方显示对于的View
     */
    public View getView(){
        if (currentViewTing == null){
            createView();
        }
        return currentViewTing;
    }


}
